package com.wenhai.spring.dependency.injection;

import com.wenhai.xiaomage.spring.ioc.overview.dependency.domain.User;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @Author:wenhai
 * @Description: {@link org.springframework.beans.factory.ObjectProvider} 演示延迟注入
 * @Date:2020/3/10
 * @see Qualifier
 */
public class LazyDependencyInjectionDemo {

    /**
     * 延迟注入
     */
    @Autowired
    private ObjectProvider<User> userObjectProvider;

    /**
     * 实时注入
     */
    @Autowired
    private User  user;


    public static void main(String[] args) {
        AnnotationConfigApplicationContext cxt = new AnnotationConfigApplicationContext();
        cxt.register(LazyDependencyInjectionDemo.class,ConfigUser.class);
        XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(cxt);
        String xmlResourcePath = "classpath:/META-INF/dependency-lookup-context.xml";
        beanDefinitionReader.loadBeanDefinitions(xmlResourcePath);
        cxt.refresh();
        LazyDependencyInjectionDemo demo = cxt.getBean(LazyDependencyInjectionDemo.class);
        System.out.println(demo.user);
        System.out.println(demo.userObjectProvider.getIfAvailable());
        for (User user : demo.userObjectProvider) {
            System.out.println(user);
        }
        cxt.close();
    }
}
